<?php

namespace App\AgentAdmin\Controllers;

use App\Models\AgentAccount;
use App\Models\Agent;
use App\Admin\Repositories\Withdraw;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Alert;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Layout\Column;

class WithdrawController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Withdraw(), function (Grid $grid) {
            $grid->model()->where('agent_id',Admin::user()->id)->orderBy('created_at','desc');
            // $grid->column('id')->sortable();
            $grid->column('number')->display(function(){
                return "<div style='color:red;font-weight:bold;'>申请金额：￥{$this->number}</div>";
            });
            $grid->column('charge');
            $grid->column('method')->using([1 => '银行卡', 2 => '微信',3=>'支付宝'])->dot([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
                1 => 'warning',
                2 => 'success',
                3 => 'primary',
            ]);
            $grid->column('receiver');
            $grid->column('bank');
            $grid->column('no');
            $grid->column('qr')->image('http://'.$_SERVER["HTTP_HOST"].'/storage/images/agents/',100,100);
            $grid->column('state')->using([0 => '已提交', -1 => '已拒绝',1=>'已打款'])->label([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
                1 => 'success',
                -1 => 'danger',
                0 => 'primary',
            ]);
            $grid->column('remark')->style('color:red;font-weight:bold;');
            $grid->column('created_at')->sortable();
            $grid->disableFilterButton();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableBatchDelete();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
            $grid->header(function ($collection) use ($grid) {
                return "<div style='padding: 10px;padding-left: 1.8rem;font-weight:bold;color:#62a8ea;'>系统自动清空3天前的记录</div> ";
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Withdraw(), function (Show $show) {
            $show->field('id');
            $show->field('agent_id');
            $show->field('number');
            $show->field('method');
            $show->field('state');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    public function create(Content $content)
    {
        
        return $content
            ->header('资金提现')
            ->body(
            function (Row $row){
                $row->column(12, function (Column $column)  {
                    $alert = Alert::make( "提现前先在资金账户完善对应收款方式的收款信息。此处不能修改收款信息",'提示');
                    $column->row($alert->info());
                    $column->row($this->form());
                });
            });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Withdraw(), function (Form $form) {
            $agent_id = Admin::user()->id;
            $account = AgentAccount::where('agent_id',$agent_id)->first();
            $max = 0;
            if($account){
                $max = $account->balance;
                // if($account->edit_code=='666666'){
                //     return $form->response()->info('请先完善收款信息')->redirect("agent-accounts/1/edit");
                // }
            }
            $form->hidden('agent_id')->default($agent_id);
            $form->currency('number')->required()->attribute('min', 500)->attribute('max', $max)->help("账户余额：￥{$max}。每次最低提现金额 ￥500.00");
            $form->radio('method')->options([1 => '银行卡', 2=>'微信',3=>'支付宝'])->default(1)
            ->when(1, function (Form $form) use($account){
                $form->text('receiver','收款人')->required()->default($account->bank_receiver)->readonly();
                $form->text('bank','银行名称')->required()->default($account->bank_name)->readonly();
                $form->text('no','银行卡号')->required()->default($account->bank_number)->readonly();
            })->when(2, function (Form $form) use($account){
                $form->image('wx_qr','收款码')->disable()->required()->default($account->wx_qr);
            })->when(3, function (Form $form) use($account){
                $form->image('ali_qr','收款码')->disable()->required()->default($account->ali_qr);
            })->help("<div style='color:red;'>具体收款信息只能在资金账号里面修改完善</div>");
            $form->hidden('qr');
            $form->hidden('charge');
            $form->disableHeader();
            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                $footer->disableReset();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
            $form->submitted(function(Form $form){
                if($form->number<500){
                    return $form->response()->alert(true)->error('请输入正确提现金额，最低提现金额500');
                }
                if($form->method==1){
                    $form->deleteInput('wx_qr');
                    $form->deleteInput('ali_qr');
                    $form->deleteInput('qr');
                    if(!boolval($form->bank) || !boolval($form->receiver) || !boolval($form->no)){
                        return $form->response()->alert(true)->error('请先完善收款银行信息，或者选择其他收款方式')->redirect("agent-accounts/1/edit");
                    }
                }
                else if($form->method==2){
                    if(!boolval($form->wx_qr)){
                        return $form->response()->alert(true)->error('请先完善微信收款码，或者选择其他收款方式')->redirect("agent-accounts/1/edit");
                    }
                    $form->qr = $form->wx_qr;
                    $form->deleteInput('ali_qr');
                    $form->deleteInput('wx_qr');
                    $form->deleteInput('bank');
                    $form->deleteInput('no');
                    $form->deleteInput('receiver');
                }else{
                    if(!boolval($form->ali_qr)){
                        return $form->response()->alert(true)->error('请先完善支付宝收款码，或者选择其他收款方式')->redirect("agent-accounts/1/edit");
                    }
                    $form->qr = $form->ali_qr;
                    $form->deleteInput('wx_qr');
                    $form->deleteInput('ali_qr');
                    $form->deleteInput('bank');
                    $form->deleteInput('no');
                    $form->deleteInput('receiver');
                }
                $agent = Agent::find($form->agent_id);
                $form->charge = bcdiv(bcmul($form->number,$agent->withdraw_rate??0,4),100,2);
            });
            $form->saved(function(Form $form)use($account){
                $account->decrement('balance', $form->number);
                $account->increment('withdrawed', $form->number);

            });
        });
    }
}
